$(function() {
	dotoTemplate(function(data) {
		//商品地址二维码生成
		var productUrl = location.href;
		console.log(productUrl, 999);
		qrcodeUrl(productUrl);

		//数据遍历
		$('#show-contain').html(template('showTemplate', data));
		//商品二维码
		$('#code-img').on('tap', function() {
			$('.code-bj').css({
				"display": "block"
			});					
		});
		$('.code-bj').on('tap', function() {
			$('.code-bj').css({
				"display": "none"
			});
		});
		//微信联系
		$('#wechat').on('tap', function() {
			$('.bottom-talk').animate({
				bottom: "1rem"
			})
		});
		$('#code-x').on('tap', function() {
			$('#bottom-talk').animate({
				bottom: "-3rem"
			})
		});
		//	收藏
		$('#bottom-btn-change').on('tap', function() {
			$.ajax({
				type: 'get',
				url: window.address + '/collect/collectSave',
				data: {
					'businessId': 1,
					'goodsId': CD.geturl().id,
					'userUuid':newBusiness
				},
				traditional: true,
				xhrFields: {
					withCredentials: true
				},
				crossDomain: true,
				dataType: 'json',
				success: function(data) {
					if(getCookieValue("token")==""){
						window.location.href = "l-userlogin.html";
					}
					
					console.log(data, 888);
					if(data == 0) {
						mui.toast('收藏成功');
					} else if(data == 1) {
						mui.toast('已在收藏夹');
					} else {
						window.location.href = "l-userlogin.html";
					}
				}
			});
		});
		//电话
		$('.phone-tel').on('tap', function(){
			var goodsId = $(this).attr('data-good');
//			var userUuid = $(this).attr('data-user');
//			console.log(goodsId, userUuid);
			phoneOrder({
				goodsId: goodsId,
				token:getCookieValue("token")
			}, function(data) {
				console.log('预约成功')
			});
		});
	});

});

//信息
var newBusiness= CD.geturl().business
if(!newBusiness){
	newBusiness=""
};
var dotoTemplate = function(callback) {
	$.ajax({
		type: "get",
		url: window.address + "/goods/findBayId",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: {
			"goodsId": CD.geturl().id,
			"userUuid": newBusiness
		},
		dataType: "json",
		success: function(data) {
			console.log(data, 999);
			callback && callback(data);
			//初始化轮播
			var gallery = mui('.mui-slider');
			gallery.slider({
				interval: 5000 //自动轮播周期，若为0则不自动播放，默认为0；
			});
		}
	});
};

//生产二维码
var qrcodeUrl = function(productUrl) {
	var qrcode = new QRCode('code-contain', //document.getElementById('#qrcode');
		{
			text: productUrl,
			width: 100,
			height: 100,
			colorDark: '#000000',
			colorLight: '#ffffff',
			correctLevel: QRCode.CorrectLevel.H
		});
}

//电话触发预约接口
var phoneOrder = function(params, callback) {
	$.ajax({
		type: "get",
		url: window.address + "/order/addOrder",
		xhrFields: {
			withCredentials: true
		},
		crossDomain: true,
		data: params,
		dataType: "json",
		success: function(data) {
			console.log(data, 999);
			callback && callback(data);
		}
	});
}